﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Mengsk.ProxyDelivery.Print.Domain.PrintDataFormatters
{
    class PrintDataFormatterGoods_VendorMark : PrintDataFormatter
    {
        private OrderService os = ServiceContainer.GetService<OrderService>();
        private VendorService vs = ServiceContainer.GetService<VendorService>();

        public override PrintTemplateItemTypes AcceptType
        {
            get { return PrintTemplateItemTypes.GOODS_VENDORMARK; }
        }

        public override string Format(PrintTemplateItem item, ProxyDelivery.Domain.Order order)
        {
            try
            {
                string vendors = "";
                if (order.OrderGoodss == null)
                {
                    return "";
                }
                foreach (var og in order.OrderGoodss.Where(obj => os.IsOrderGoodsValid(obj.State)))
                {
                    var vendor = this.vs.GetByName(og.Vendor);
                    if (vendor == null || vendor.Count < 1)
                    {
                        MessageBox.Show("厂家不存在");
                        continue;
                    }

                    if (vendor.Count > 1)
                    {
                        MessageBox.Show("找到多个厂家,订单编号：" + order.Id + "厂家名称:" + og.Vendor);
                    }

                    string doorNumber = VendorService.FormatVendorDoor(vendor[0].MarketAddress);
                    vendors += doorNumber + " ";
                }
                return vendors;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return "";
        }
    }
}
